package com.rgc.dao;

import java.sql.ResultSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.rgc.domain.Agrupacion;
import com.rgc.domain.Comuna;
import com.rgc.domain.Region;

public class LocalidadDaoImpl  extends JdbcDaoSupport implements LocalidadDao{

	public Map<String,String> getRegion() {
		try{
		String sql = " select * from region  ";
		List<Region> regiones=getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Region>(Region.class));
		Map<String,String> listado = new LinkedHashMap<String,String>();
		listado.put("0", "--Region--");
		if(regiones!=null){		
			for(Region region : regiones)
				listado.put(region.getIdregion(), region.getNombre());	
		}
		return listado;
		}catch(Exception e){
			e.printStackTrace();
		}
		
		return null;
	}

	public Map<String,String> getComuna(String idregion) {
		
		try{
			String sql = " select * from comuna where idregion='"+idregion+"'  ";
			List<Comuna> comunas=getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Comuna>(Comuna.class));
			Map<String,String> listado = new LinkedHashMap<String,String>();
			listado.put("0", "--Comuna--");
			if(comunas!=null){		
				for(Comuna comuna : comunas)
					listado.put(comuna.getIdcomuna(), comuna.getNombre());	
			}
			return listado;
			
			}catch(Exception e){
				e.printStackTrace();
			}
			
		return null;
	}
	
public List<Comuna> getComunalist(String idregion) {
		
		try{
			String sql = " select * from comuna where idregion='"+idregion+"'  ";
			List<Comuna> comunas=getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Comuna>(Comuna.class));
			
			return comunas;
			
			}catch(Exception e){
				e.printStackTrace();
			}
			
		return null;
	}

public Region getLaRegion(String idregion) {
	try{
	String sql = " select * from region  where idregion='"+idregion+"' ";
	System.out.println(sql);
	List<Region> region= getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Region>(Region.class));
	return region.get(0);
	}catch(Exception e){
		
		e.printStackTrace();
	}
	return null;
}

public Comuna getLaComuna(String idcomuna) {
	try{
		String sql = " select * from comuna  where idcomuna='"+idcomuna+"' ";
		List<Comuna> comuna= getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Comuna>(Comuna.class));
		return comuna.get(0);
		}catch(Exception e){
			
			
		}
		return null;

}

public String getModalidad(String idmodalidad) {
	try{
		String sql = " select * from modalidad  where idmodalidad='"+idmodalidad+"' ";
		List<Comuna> comuna= getJdbcTemplate().query(sql, new BeanPropertyRowMapper<Comuna>(Comuna.class));
		
		return comuna.get(0).getNombre();
		}catch(Exception e){
			
			
		}
		return null;

}

}
